using UnityEngine;

public class PlayerDashState : EntilyState
{

    private float originalGravityScale;
    private int dashDir;
    public PlayerDashState(StateMachine stateMachine, string animBoolName, Player player) : base(stateMachine, animBoolName, player)
    {
    }
    public override void Enter()
    {
        base.Enter();
        dashDir = player.moveInput.x != 0 ? ((int)player.moveInput.x) : player.facingDir;
        stateTimer = player.dashDuration;
        originalGravityScale = rb.gravityScale;
        rb.gravityScale = 0;
    }

    public override void UpDate()
    {
        base.UpDate();

        player.SetVelocity(player.dashSpeed * dashDir, 0);

        //if (player.wallDetected)
        //{
        //    stateMachine
        //}
        CancelDashIfNeeded();

        if (stateTimer<0)
        {
            if (player.groundDetected)
            {
                stateMachine.ChangeState(player.playerIdleState);
            }
            else
            {
                stateMachine.ChangeState(player.playerFallState);
            }
                
        }
    }

    public override void Exit() 
    {
        base.Exit();
        rb.gravityScale = originalGravityScale;
        player.SetVelocity(0, 0);
    }

    private void CancelDashIfNeeded()
    {
        if (player.wallDetected)
        {
            if (player.groundDetected)
            {
                stateMachine.ChangeState(player.playerIdleState);
            }else
            {
                stateMachine.ChangeState(player.playerWallSlideState);
            }
        }
    }

   
}
